<template>
  <el-dialog :visible.sync="addVisible" width="55%" :show-close='false' :closeOnClickModal='false' class="printTable">
    <span slot="title" class="dialog-title">
				选择打印模板
				<el-button @click="close" size="mini" class="popupClose">关闭</el-button>
			</span>
    <el-table :data="printTemplateLists" size="mini" highlight-current-row ref="printLists" class="printLists">
      <el-table-column min-width="45px" fixed label="序号">
        <template slot-scope="scope">
          {{scope.$index+1}}
        </template>
      </el-table-column>
      <el-table-column label="模板名称" prop="name">

      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="submitPrint(scope.row)">打印</el-button>
          <!--<el-button type="text" size="mini" @click="submitPrint(scope.row)">预览</el-button>-->
        </template>
      </el-table-column>


    </el-table>
  </el-dialog>
</template>

<script>
  import { mapState } from 'vuex'
  import $ from "jquery"

  export default {
    data() {
      return {
        printTemplateLists: [],


      }
    },
    computed: {
      ...mapState(['enableStatus', 'tableHeightA', 'yesorno', 'enableStatus', 'caiyangrenData']),
    },
    props: ['addVisible', 'printTemplateList', 'printData'],
    watch: {
      addVisible(val) {
        if (val) {
          this.printTemplateLists = this.printTemplateList
          // 如果有默认的模板就直接调用
          const defaultTemplate = this.printTemplateLists.find(ele => ele.defaultFlag)
          if(defaultTemplate) {
            this.submitPrint(defaultTemplate)
          }
        }
      }
    },
    methods: {
      close() {
        this.$emit('update:addVisible', false);

      },

      // 是否重新加载
      reLoad() {
        this.reload = false;
        this.$nextTick(() => {
          this.reload = true;
        })
      },
      submitPrint(row) {
        // printNum
        var sampleArr = [];
        for (var i in this.printData) {
          // var obj = {};
          // obj.id = this.printData[i].id;
          // obj.printNum = this.printData[i].printNum;
          this.printData[i].printTemplateId = row.id;
          // sampleArr.push(obj)
        }
        var that = this;
        this.$http({
          method: 'post',
          apiUrl: '/detection/printSampleById.htm',
          params: this.printData,
          isJson: true
        }).then(res => {
          if (res.code === 0) {
        		this.$utils.isInstallPrintSoftware(res.data);
            // $.ajax({
            //   type: "GET",
            //   url: res.data,
            //   success: function (test) {
            //     if (test != 'ok') {
            //       that.$notify({
            //         title: "警告",
            //         message: "请安装打印插件",
            //         type: "warning"
            //       })
            //     }
            //   },
            //   error: function () {
            //     that.$notify({
            //       title: "警告",
            //       message: "请安装打印插件",
            //       type: "warning"
            //     })
            //   }
            // });
          }
        })

      },

    }
  }

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
  .printTable .el-dialog {
    /*margin-top: 10vh !important;*/
    height: 60%;
  }

  .printTable .el-dialog .el-dialog__body {
    height: calc(100% - 85px);
    padding: 10px;
    overflow: auto
  }
</style>